'''
陈超依 19377189
现代程序设计第二周作业
'''

import jieba
from wordcloud import WordCloud as W
from matplotlib import pyplot as plt

#加工文本并进行简单分析
def comment_process():
    #文件写入
    Comments = open("E:/Py_Programs/week2/jd_Comments.txt",mode='r',encoding='utf-8').read()
    Words = jieba.lcut(Comments)
    StopWords = open("E:/Py_Programs/week2/StopWords_list.txt",mode='r',encoding='utf-8').read()
    StopWords = StopWords.split("\n")
    print("文本写入已完成\n")

    #统计词频
    Count = { }
    for word in Words:
        if word not in StopWords:
            if word in Count:
                Count[word] = Count[word]+1
            else:
                Count[word] = 1
    Count.pop('\n')
    Count.pop(' ')
    print("词频统计已完成\n")

    #筛出频率大于五十的词
    Ltemp = [ ]
    for key in Count:
        if Count[key]<50:
            Ltemp.append(key)
    for key in Ltemp:
        Count.pop(key)
    print("词频筛选已完成\n")
    print(Count)

    #词云图
    #首先人工处理掉部分无效词
    Count.pop('很')
    Count.pop('好')
    Count.pop('都')
    Count.pop('不')
    WordL = [ ]
    for key in Count:
        for i in range(Count[key]):
            WordL.append(key)
    WordS = ' '.join(WordL)
    font = r'C:/Windows/Fonts/FZSTK.TTF'
    WC = W(font_path=font, background_color='white', 
    width=1000, height=800, collocations=False).generate(WordS)
    plt.imshow(WC)
    plt.axis('off')
    plt.show()

    #基于特征集对每条评论分析
    KeywordNumber = len(Count) #关键词个数
    KeywordList = [ k for k,value in Count.items()]  #关键词列表
    CommentsVector = [ ]
    CommentsDist = [ ]  #评论向量距其他评论向量的距离 列表
    CommentFile = open("E:/Py_Programs/week2/jd_Comments.txt",mode='r',encoding='utf-8').readlines()
    CommentsNumber = len(CommentFile)
    #将每个评论向量化，装入CommentsVector
    for c in CommentFile:
        c=jieba.lcut(c)
        CL=[0]*KeywordNumber  #评论向量
        for i in range(KeywordNumber):
            if KeywordList[i] in c:
                CL[i]=1
        CommentsVector.append(CL)
    #计算每个评论向量距其他向量的距离，装入CommentsDist
    print("开始计算各评论间距离")
    for i in range(CommentsNumber):
        dist = 0
        for j in range(CommentsNumber):
            for dimension in range(KeywordNumber):
                dist+=abs(((CommentsVector[i])[dimension])-((CommentsVector[j])[dimension]))
        CommentsDist.append(dist)
        if i%100 == 0:
            print('已完成%i条评论计算'%i)


    #寻找距离其他评论最小的评论 并输出
    MinDist = 0
    for i in range(KeywordNumber):
        if CommentsDist[i]<CommentsDist[MinDist]:
            MinDist = i
    print(CommentFile[MinDist])
    
def main():
    comment_process()

if __name__ == '__main__':
    main()